<?php

namespace App\Inventor\Controllers;

use App\Extensions\Actions\Post\GrantPatentPost;
use App\Extensions\Actions\Upload\PatentApplyFileUploadAction;
use App\Extensions\Actions\Upload\PatentCertUploadAction;
use App\Http\Controllers\Controller;
use App\Repositories\PatentApplyRepository;
use App\Services\PatentApplyService;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Layout\Content;
use PhpOffice\PhpWord\IOFactory;


class PatentApplyController extends Controller
{
    protected $title = '专利申请前登记';
    public $repository;
    protected $service;

    public function __construct(PatentApplyRepository $repository, PatentApplyService $service)
    {
        $this->service = $service;
        $this->repository = $repository;
    }

    public function index(Content $content)
    {
        $user = Admin::user();
        $grid = $this->repository->userGrid();
        $this->service->gridColumns($grid, 0, 0);
        $grid->column('user.name','创建人')->width(80);
        $grid->column('created_at','创建时间')->display(function($value){
            return Carbon::parse($value)->toDateString();
        })->width(90);
        $grid->actions(function (Grid\Displayers\Actions $actions) use($user) {
            $actions->disableView();
            $row = $actions->row;
            if($row['user_id'] != $user['id']){
                $actions->disableAll();
            }
            if ($row->dept_review_status == 1 && in_array($row->review_status, [0, 1, 2])) {
                $actions->disableEdit();
                $actions->disableDelete();
            }
            if (!$row->review_status) {
                if (!$row->file) {
                    $actions->prepend(new PatentApplyFileUploadAction($actions));
                }
            } elseif ($row->review_status == 1) {
                if (!$row->status) {
                    $actions->prepend(new GrantPatentPost($actions));
                }
            }
        });
        $_selector = request()->get('_selector');
        $review_status = $_selector['review_status'] ?? 0;
        if (in_array($review_status, [1, 2])) {
            $grid->disableActions();
        }
        return $content
            ->title($this->title)
            ->description(trans('admin.list'))
            ->body(view('list-remark', [
                'title'   => '提示',
                'message' => collect([
                    '<strong>专利申请流程：</strong>申请前登记',
                    '科室审批',
                    '意向代理机构-检索',
                    '院级审批',
                    '申请专利',
                    '职工上传证书',
                    '已授权',
                ])->implode('&nbsp;&nbsp;<i class="fa fa-arrow-right"></i>&nbsp;&nbsp;')
            ]))
            ->body($grid);
    }

    public function publish(Content $content)
    {
        $user = Admin::user();
        $grid = $this->repository->userGrid();
        $grid = $this->service->gridColumns($grid, 0, 1);
        $grid->disableCreateButton();
        $grid->actions(function (Actions $actions) use($user){
            $actions->disableView();
            $actions->disableDelete();
            $actions->disableEdit();
            if ($actions->row->review_status == 1 && !$actions->row->cert) {
                $actions->prepend(new PatentCertUploadAction($actions));
            }
            if($actions->row->user_id != $user['id']){
                $actions->disableAll();
            }
        });
        return $content->title('已授权专利')->body($grid);
    }

    public function create(Content $content)
    {
        return $content
            ->title($this->title)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    public function store()
    {
        return $this->form()->store();
    }

    public function edit($id, Content $content)
    {
        $model = $this->repository->findOrFail($id);
        $this->authorize('edit', $model);
        return $content
            ->title($this->title)
            ->description(trans('admin.edit'))
            ->body($this->form($model)->edit($id));
    }

    public function update($id)
    {
        $model = $this->repository->find($id);
        $this->authorize('update', $model);
        return $this->form()->update($id);
    }

    protected function form($model = null)
    {
        return $this->service->form($model);
    }

    public function download($id)
    {
        $model = $this->repository->find($id);
        if (!$model) {
            return response()->view('errors.404');
        }
        return $this->service->downloadTable($model);
    }

    public function preview($id)
    {
        $model = $this->repository->find($id);
        $templateProcessor = $this->service->tableTemplateProcessor($model);
        $wordfile = IOFactory::load($templateProcessor->save());
        $xmlWriter = IOFactory::createWriter($wordfile, "HTML"); // 转换为html
        $html = $xmlWriter->getContent(); // html内容获取
        echo $html;
    }

    public function destroy($id)
    {
        $model = $this->repository->findOrFail($id);
        if (!policy_check('delete', $model)) {
            return response()->json([
                'status'  => false,
                'message' => '删除失败'
            ]);
        }
        return $this->form()->destroy($id);
    }
}
